/**
 * 
 */
package com.ynet.middleground.riskmanage.service.impl;

import java.util.List;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.ynet.middleground.riskmanage.dto.qdg.OneGoodsInfoDTO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ynet.core.common.PagerInfo;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.riskmanage.bean.DgCustomsReleaseReq;
import com.ynet.middleground.riskmanage.bean.newrizhaogang.NewRzgControlCargoAsynReq;
import com.ynet.middleground.riskmanage.bean.newrizhaogang.NewRzgControlCargoQueryReq;
import com.ynet.middleground.riskmanage.bean.newrizhaogang.NewRzgControlCargoReq;
import com.ynet.middleground.riskmanage.bean.newrizhaogang.NewRzgRegisterCustomerReq;
import com.ynet.middleground.riskmanage.bean.rizhaogang.*;
import com.ynet.middleground.riskmanage.bean.ytg.YtgControlCargoQueryReq;
import com.ynet.middleground.riskmanage.bean.ytg.YtgQueryTransferResultReq;
import com.ynet.middleground.riskmanage.dto.rizhaogang.*;
import com.ynet.middleground.riskmanage.dto.ytg.YtgTransferResultDTO;
import com.ynet.middleground.riskmanage.model.DaGangModel;
import com.ynet.middleground.riskmanage.model.rizhaogang.NewRiZhaoGangModel;
import com.ynet.middleground.riskmanage.model.rizhaogang.RiZhaoGangModel;
import com.ynet.middleground.riskmanage.service.NewRiZhaoGangService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName NewRiZhaoGangServiceImpl
 * @Description 对接新日照港服务实现类
 * @Author songyf
 * @Date 2022/05/12
 * @Version 1.0
 */

@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Api(value = "对接新日照港服务实现类", tags = {"对接新日照港服务实现类"})
@Path("newrizhaogang")
@Service(timeout = 6000, version = "0.0.1")
public class NewRiZhaoGangServiceImpl implements NewRiZhaoGangService, GenericService {
    @Autowired
    NewRiZhaoGangModel newRiZhaoGangModel;

    @Autowired
    RiZhaoGangModel riZhaoGangModel;

    @Autowired
    DaGangModel daGangModel;

    /**
     * 日照港请求“海关放行信息查询”接口传入参数
     */
    private static final String RIZHAOGANG_REQ_PARAM = "0";
    /**
     * 大港请求“海关放行信息查询”接口传入参数
     */
    private static final String DAGANG_REQ_PARAM = "1";

    @POST
    @Path("register-customer")
    @ApiOperation(value = "注册(更名)用户信息服务", notes = "注册(更名)用户信息服务", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "NewRzgRegisterCustomerReq", paramType = "body", required = true)
    @Override
    public ServiceResult<Object> registerCustomer(NewRzgRegisterCustomerReq req) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            result.setResult(newRiZhaoGangModel.registerCustomer(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "注册(更名)用户信息服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    @POST
    @Path("queryAllGoods")
    @ApiOperation(value = "货物全量查询", notes = "货物全量查询(若查询日照港：作业委托人ID和作业委托人名称必填)", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "RzgAllGoodsInfoQueryReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<RzgNewGoodsInfoDTO>> queryAllGoods(RzgAllGoodsInfoQueryReq req) {
        ServiceResult<List<RzgNewGoodsInfoDTO>> result = new ServiceResult<>();
        try {
            if ("0".equals(req.getChanType())) {
                result.setResult(newRiZhaoGangModel.queryAllGoods(req));
            }
            if ("1".equals(req.getChanType())) {
                result.setResult(newRiZhaoGangModel.queryAllGoodsQDG(req));
            }
            if ("2".equals(req.getChanType())) {
                PagerInfo pagerInfo = new PagerInfo();
                result.setResult(newRiZhaoGangModel.queryAllGoodsYTG(req, pagerInfo));
                pagerInfo.setPageSize(req.getRows());
                pagerInfo.setPageNum(req.getPage());
                result.setPagerInfo(pagerInfo);
            }
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "货物全量查询服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;

    }

    @POST
    @Path("queryPartsGoods")
    @ApiOperation(value = "货物非全量查询", notes = "货物非全量查询", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "RzgPartGoodsInfoQueryReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<RzgNewGoodsInfoDTO>> queryPartsGoods(RzgPartGoodsInfoQueryReq req) {
        ServiceResult<List<RzgNewGoodsInfoDTO>> result = new ServiceResult<>();
        try {
            result.setResult(newRiZhaoGangModel.queryPartGoods(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "货物非全量查询服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;

    }

    @POST
    @Path("queryOneGoods")
    @ApiOperation(value = "货物单货键查询", notes = "货物单货键查询", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "RzgOneGoodsInfoQueryReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<OneGoodsInfoDTO>> queryOneGoods(RzgOneGoodsInfoQueryReq req) {
        ServiceResult<List<OneGoodsInfoDTO>> result = new ServiceResult<>();
        try {
            if ("0".equals(req.getChanType())) {
                result.setResult(newRiZhaoGangModel.queryOneGoods(req));
            }
            if ("1".equals(req.getChanType())) {
                result.setResult(newRiZhaoGangModel.queryOneGoodsQdg(req));
            }
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "货物单货键查询服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;

    }

    @POST
    @Path("dealTransferCommand")
    @ApiOperation(value = "发起电子仓单转让指令", notes = "发起电子仓单转让指令", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "RzgTransferCommandReq", paramType = "body", required = true)
    @Override
    public ServiceResult<RzgTransferResultDTO> dealTransferCommand(RzgTransferCommandReq req) {
        ServiceResult<RzgTransferResultDTO> result = new ServiceResult<>();
        try {
            if ("0".equals(req.getChanType())) {
                result.setResult(newRiZhaoGangModel.dealTransferCommand(req));
            }
            if ("2".equals(req.getChanType())) {
                result.setResult(newRiZhaoGangModel.dealYtgTransferCommand(req));
            }
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "发起电子仓单转让指令服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    @POST
    @Path("receiveTransferResult")
    @ApiOperation(value = "接收日照港转让指令处理结果", notes = "接收日照港转让指令处理结果", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "RzgReceiveTransferResultReq", paramType = "body",
        required = true)
    @Override
    public ServiceResult<Object> receiveTransferResult(RzgReceiveTransferResultReq req) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            result.setResult(newRiZhaoGangModel.receiveTransferResult(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "接收电子仓单转让指令处理结果服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    @POST
    @Path("cargo-control")
    @ApiOperation(value = "发起控货(解除)指令服务", notes = "发起控货(解除)指令服务", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "NewRzgControlCargoReq", paramType = "body", required = true)
    @Override
    public ServiceResult<NewRzgCargoControlResultDTO> controlCargo(NewRzgControlCargoReq req) {
        ServiceResult<NewRzgCargoControlResultDTO> result = new ServiceResult<>();
        try {
            result.setResult(newRiZhaoGangModel.controlCargo(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "发起控货(解除)指令服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    @POST
    @Path("cargo-control-query")
    @ApiOperation(value = "控货指令结果查询服务", notes = "控货指令结果查询服务", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "NewRzgControlCargoReq", paramType = "body", required = true)
    @Override
    public ServiceResult<NewRzgCargoControlQueryResultDTO> controlCargoQuery(NewRzgControlCargoQueryReq req) {
        ServiceResult<NewRzgCargoControlQueryResultDTO> result = new ServiceResult<>();
        try {
            result.setResult(newRiZhaoGangModel.controlCargoQuery(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "控货指令结果查询服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    @POST
    @Path("cargo-control-query-ytg")
    @ApiOperation(value = "控货指令结果查询服务(烟台港)", notes = "控货指令结果查询服务(烟台港)", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "NewRzgControlCargoReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<YtgCargoControlQueryResultDTO>> controlCargoQueryYtg(YtgControlCargoQueryReq req) {
        ServiceResult<List<YtgCargoControlQueryResultDTO>> result = new ServiceResult<>();
        try {
            result.setResult(newRiZhaoGangModel.controlCargoQueryYtg(req));
            result.setPagerInfo(req.getPagerInfo());
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "控货指令结果查询(烟台港)服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    @POST
    @Path("cargo-control-asyn")
    @ApiOperation(value = "控货结果异步通知(日照港向金控中台推送控货结果数据)服务", notes = "控货结果异步通知(日照港向金控中台推送控货结果数据)服务", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "NewRzgControlCargoAsynReq", paramType = "body", required = true)
    @Override
    public ServiceResult<Object> controlCargoAsyn(NewRzgControlCargoAsynReq req) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            result.setResult(newRiZhaoGangModel.controlCargoAsyn(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "控货结果异步通知服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 海关放行信息查询
     *
     * @author songyf
     * @param req 查询海关放行信息请求信息
     * @return 海关放行信息传输对象
     */
    @POST
    @Path("customs-release")
    @ApiOperation(value = "日照港、大港查询海关放行信息", notes = "日照港、大港查询海关放行信息")
    @ApiImplicitParam(name = "requestObj", dataType = "RzgCustomsReleaseQueryReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<RzgCustomsReleaseDTO>> getRzgCustomsRelease(@Valid RzgCustomsReleaseQueryReq req) {
        ServiceResult<List<RzgCustomsReleaseDTO>> result = new ServiceResult<>();
        try {
            if (RIZHAOGANG_REQ_PARAM.equals(req.getSysFlag())) {
                result.setResult(riZhaoGangModel.getCustomsRelease(req));
            } else if (DAGANG_REQ_PARAM.equals(req.getSysFlag())) {
                DgCustomsReleaseReq reqDg = new DgCustomsReleaseReq();
                reqDg.setTraceId(req.getTraceId());
                reqDg.setChannel(req.getChannel());
                reqDg.setOperationUserId(req.getOperationUserId());
                reqDg.setRequestTime(req.getRequestTime());
                reqDg.setCustomsNo(req.getEntryId());
                result.setResult(daGangModel.getCustomsRelease(reqDg));
            } else {
                result.setError("ECCT0000", "系统标识错误");
            }
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "日照港、大港查询海关放行信息服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 烟台港转让结果查询
     *
     * @author liwq
     * @param req 烟台港转让结果查询请求信息
     * @return 烟台港转让结果查询响应信息
     */
    @POST
    @Path("queryTransferResult")
    @ApiOperation(value = "烟台港转让结果查询", notes = "烟台港转让结果查询", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "YtgQueryTransferResultReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<YtgTransferResultDTO>> queryTransferResult(YtgQueryTransferResultReq req) {
        ServiceResult<List<YtgTransferResultDTO>> result = new ServiceResult<>();
        try {
            result.setResult(newRiZhaoGangModel.queryTransferResult(req));
            result.setPagerInfo(req.getPagerInfo());
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "烟台港转让结果查询服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }
}
